package com.browsesoft.adapter;

import java.util.Hashtable;
import java.util.LinkedList;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.w3c.dom.Element;

import com.browsesoft.EntityManager;
import com.browsesoft.ExtendElement;
import com.browsesoft.LoginModel;
import com.browsesoft.ManagerGroup;
import com.browsesoft.user.EncryptTools;
import com.browsesoft.user.User;

/**
 * <p>
 * Title:
 * </p>
 * <p>
 * Description:
 * </p>
 * <p>
 * Copyright: Copyright (c) 2004
 * </p>
 * <p>
 * Company:
 * </p>
 * 
 * @author not attributable
 * @version 1.0
 */
public class LoginModelAdapter extends LoginModel {
	public LoginModelAdapter() {
	}

	public boolean checkPassword(HttpServletRequest request) throws Exception {
		// 验证用户
		Hashtable ht = new Hashtable();
		// 得到登陆用户姓名
		String loginUser = request.getParameter("loginUser");
		// ekey用户
		String eKeyUser = request.getParameter("ekeyuser");
		// 将登录名放到哈希表
		ht.put("ename", loginUser);
		// 将密码放到哈息表
		// 取得用户输入的密码
		String pw = request.getParameter("password");
		// 加密
		pw = EncryptTools.getEncryptDataByRight(pw, "userpassword");
		ht.put("password", pw);
		// 取出属性名=属性值对
		ht.put("f_using", "是");
		LinkedList users = ((EntityManager) ManagerGroup.getManagerGroup()
				.getManager("entity")).getObjects("com.browsesoft.user.User",
				ht);
		// 如果用户正确
		if (users.size() > 0) {
			User user = (User) users.get(0);
			// 如果用户是ekey用户
			String isEKeyUser = (String) user.getAttributes().get("ekeyuser");
			if (isEKeyUser != null && isEKeyUser.equals("true")) {
				// 如果不相等
				if (!loginUser.equals(eKeyUser)) {
					return false;
				}
			}
			// 把登陆用户放到session中
			request.getSession().setAttribute("loginUser", user);
			// 登陆用户登陆次数加1
			user.incLoginNumber();
			return true;
		} else {
			// 将登录名放到哈希表
			ht.put("ename", eKeyUser);
			users = ((EntityManager) ManagerGroup.getManagerGroup().getManager(
					"entity")).getObjects("com.browsesoft.user.User", ht);
			// 如果用户正确
			if (users.size() > 0) {
				User user = (User) users.get(0);
				// 把登陆用户放到session中
				request.getSession().setAttribute("loginUser", user);
				// 登陆用户登陆次数加1
				user.incLoginNumber();
				return true;
			}
			return false;
		}
	}

	public void service(HttpServletRequest request, HttpServletResponse response)
			throws Exception {
		this.request = request;
		// 如果请求中没有用户名
		if (request.getParameter("loginUser") == null) {
			String t = this.request.getParameter("templatename");
			// 设置IP地址
			String IP = request.getServerName() + ":" + request.getServerPort();
			if (t != null && !t.equals("")) {
				Element element = this.component.getElement();
				LinkedList ekeyObject = ExtendElement.findElements(element,
						"id", "BSEkey");
				if (ekeyObject.size() == 0) {
					throw new RuntimeException("找不到必须的元素，id='BSEkey'");
				}
				Element obj = (Element) ekeyObject.get(0);
				// 设置codebase属性
				String codebase = obj.getAttribute("codebase");
				codebase = codebase
						.replaceAll("http:///", "http://" + IP + "/");
				obj.setAttribute("codebase", codebase);
			}
			// 第一次进，不做检查
			return;
		}
		Element ce = this.component.getElement();
		Element e = (Element) this.component.getPage().getDocument()
				.getElementsByTagName("body").item(0);
		// 密码正确
		if (checkPassword(request)) {
			// 设置Form提交值
			Element ee = (Element) this.component.getPage().getDocument()
					.getElementsByTagName("form").item(0);
			String action = ce.getAttribute("topage");
			if (action == null || action.equals("")) {
				action = "main.jsp";
			}
			ee.setAttribute("action", action);
			// 进入页面时提交
			String correctcode = ce.getAttribute("correctcode");
			if (correctcode == null || correctcode.equals("")) {
				correctcode = "document.forms[0].submit()";
			}
			e.setAttribute("onload", correctcode);
		}
		// 密码错误
		else {
			String errorcode = ce.getAttribute("errorcode");
			if (errorcode == null || errorcode.equals("")) {
				errorcode = "alert('密码不正确')";
			}
			// 进入页面时，提示密码错误
			e.setAttribute("onload", errorcode);
		}
	}
}